package com.cloudfun.campusshare.service.handler;

import com.cloudfun.campusshare.common.constant.DelayConsumeAction;
import com.cloudfun.campusshare.common.constant.DelayMsgType;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.sender.DelayMessage;
import com.cloudfun.campusshare.service.DeviceOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/6/18 19:58
 * @description : 用户支付后选择超时
 */
@Slf4j
@Component
public class UserPickAfterPayMessageHandler implements DelayMessageHandler {

    @Autowired
    private DeviceOrderService deviceOrderService;

    @Override
    public DelayMsgType support() {
        return DelayMsgType.EXPIRE_USER_PICK;
    }

    @Override
    @Transactional
    public void handleMessage(DelayMessage message) {
        switch (message.getConsumerType()) {
            case START_DEVICE:
                try {
                    prepareUserContext(message);
                    BaseResult<?> startDeviceRes = deviceOrderService.startDevice(message.getOrderId());
                    log.info("延迟任务：{}启动设备结果：{}", message, startDeviceRes);
                } finally {
                    destroyUserContext();
                }
                break;
            case OPERATE_LOCK:
                deviceOrderService.transferOrReleaseDeviceLock(message.getDeviceId());
                break;
        }
    }
}
